package calin.podcast;

import android.app.TabActivity;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.KeyEvent;
import calin.podcast.MusicUtils.ServiceToken;

public class CalinPodcast extends TabActivity
    implements MusicUtils.Defs {

    private ServiceToken mToken;

    public CalinPodcast() {
    }

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        int activeTab = MusicUtils.getIntPref(this, "activetab", R.id.artisttab);
        if (activeTab != R.id.artisttab
                && activeTab != R.id.albumtab
                && activeTab != R.id.songtab
                && activeTab != R.id.playlisttab) {
            activeTab = R.id.artisttab;
        }
        MusicUtils.activateTab(this, activeTab);
        
        String shuf = getIntent().getStringExtra("autoshuffle");
        if ("true".equals(shuf)) {
            mToken = MusicUtils.bindToService(this, autoshuffle);
        }

//		try {
//			final TabHost tabHost = getTabHost();
//
//			Bundle bundle = getIntent().getExtras();
//			Intent intentPodcastList = new Intent(this, CalinPodcastList.class);
//			if (bundle != null)
//				intentPodcastList.putExtras(bundle);
//			tabHost.addTab(tabHost.newTabSpec("local").setIndicator("list")
//					.setContent(intentPodcastList));
//
//			tabHost.addTab(tabHost.newTabSpec("top").setIndicator("Top list")
//					.setContent(intentPodcastList));
//
//			// This tab sets the intent flag so that it is recreated each time
//			// the tab is clicked.
//			tabHost.addTab(tabHost
//					.newTabSpec("category")
//					.setIndicator("category")
//					.setContent(intentPodcastList));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			Log.d("Test", "Do you want to quit?"+event.getKeyCode()+"="+KeyEvent.KEYCODE_BACK);
			Log.d("Test", "Action?"+event.getAction()+"="+KeyEvent.ACTION_DOWN+":"+KeyEvent.ACTION_UP);
			if (event.getAction() == KeyEvent.ACTION_DOWN
	                && event.getRepeatCount() == 0) {
				onDestroy();
				System.exit(0);
	            return true;

	        } else if (event.getAction() == KeyEvent.ACTION_UP) {
	            if (event.isTracking() && !event.isCanceled()) {

	                // DO BACK ACTION HERE
	                return true;

	            }
	        }
			
			return true;
		} else {
			return super.dispatchKeyEvent(event);
		}
	}

	@Override
	public void onDestroy() {
        if (mToken != null) {
            MusicUtils.unbindFromService(mToken);
        }
        
		super.onDestroy();
	}

    private ServiceConnection autoshuffle = new ServiceConnection() {
        public void onServiceConnected(ComponentName classname, IBinder obj) {
            // we need to be able to bind again, so unbind
            try {
                unbindService(this);
            } catch (IllegalArgumentException e) {
            }
            IMediaPlaybackService serv = IMediaPlaybackService.Stub.asInterface(obj);
            if (serv != null) {
                try {
                    serv.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
                } catch (RemoteException ex) {
                }
            }
        }

        public void onServiceDisconnected(ComponentName classname) {
        }
    };
}